package cn.shuto.liferay.plugin.chart.portlet.datasourcecfg;

import java.io.IOException;
import java.sql.Connection;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;

import cn.shuto.liferay.plugin.chart.service.model.DataSourceCfg;
import cn.shuto.liferay.plugin.chart.service.service.DataSourceCfgLocalServiceUtil;
import cn.shuto.liferay.plugin.chart.service.util.DataSourceFactory;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class DataSourceCfgPortlet
 */
public class DataSourceCfgPortlet extends MVCPortlet {
	
	/*
	 * 创建一个新的或修改一个已存在的addOrUpdateDataSourceCfg
	 */
	public void addOrUpdateDataSourceCfg(ActionRequest request, ActionResponse response)
			throws PortalException, SystemException, IOException {
		ServiceContext serviceContext = ServiceContextFactory.getInstance(
				DataSourceCfg.class.getName(), request);
		
		long dataSourceId = ParamUtil.getLong(request, "dataSourceId");
		
		String name = ParamUtil.getString(request, "name");
		String description = ParamUtil.getString(request, "description");
		String driver = ParamUtil.getString(request, "driver");
		String connUrl = ParamUtil.getString(request, "connUrl");
		String dbusername = ParamUtil.getString(request, "dbusername");
		String dbpassword = ParamUtil.getString(request, "dbpassword");

		if(Validator.isNull(dataSourceId)){
			DataSourceCfgLocalServiceUtil.addDataSourceCfg(name, description, driver, connUrl, dbusername, dbpassword, serviceContext);
		}else{
			DataSourceCfgLocalServiceUtil.updateDataSourceCfg(dataSourceId, name, description, driver, connUrl, dbusername, dbpassword, serviceContext);
		}
		
		
	}
	public void deleteDataSourceCfg(ActionRequest request, ActionResponse response)
			throws PortalException, SystemException {
		long dataSourceId = ParamUtil.getLong(request, "dataSourceId");
		
		DataSourceCfgLocalServiceUtil.deleteDataSourceCfg(dataSourceId);
	}
	
	
	public void testDataSourceCfg(ActionRequest request, ActionResponse response)
			throws PortalException, SystemException {
		long dataSourceId = ParamUtil.getLong(request, "dataSourceId");
		if(Validator.isNotNull(dataSourceId)){
				Connection conn = DataSourceFactory.getConnection(StringUtil.valueOf(dataSourceId), true);
				if(conn!=null){
					SessionMessages.add(request, "db_test_success");
					DataSourceFactory.close(conn);
				}else{
					SessionErrors.add(request, "db_test_fail");
				}
		}else{
			SessionErrors.add(request, "db_test_fail");
		}
	}
}
